package com.shoppingcart.controller;


import com.code.domain.AjaxResult;
import com.shoppingcart.entity.Shoppingcart;
import com.shoppingcart.entity.vo.ShoppingcartVo;
import com.shoppingcart.service.IShoppingcartService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guanfenga@yonyou.com
 * @since 2023-02-15
 */
@ApiOperation(value = "购物车",notes = "购物车")
@Controller // 相当于控制层类上加@Controller + 方法上加 @ResponseBody
@Slf4j
@CrossOrigin
@RequestMapping("/shoppingcart")
public class ShoppingcartController {

    @Autowired
    private IShoppingcartService shoppingcartService;

    @ApiOperation(value = "根据用户id查询购物车信息",notes = "根据用户id查询购物车信息")
    @GetMapping(value = "/queryShoppingcartToUserId/{userId}")
    public @ResponseBody AjaxResult queryShoppingcartToUserId(@PathVariable String userId ){
        List<ShoppingcartVo> shoppingcarts =  shoppingcartService.queryShoppingcartToUserId(userId);
        return AjaxResult.success("操作成功",shoppingcarts);
    }

    @ApiOperation(value = "添加购物车",notes = "添加购物车")
    @PostMapping(value = "/saveShoppingcart")
    public @ResponseBody AjaxResult saveShoppingcart(@RequestBody Shoppingcart shoppingcart){
        String flag = "";
       try{
           int result = shoppingcartService.saveShoppingcart(shoppingcart);
           if(result == 1){
               flag = "新增成功";
           }else {
               flag = "新增失败";
           }
       }catch (Exception e){
           log.info("操作异常====>",e.getMessage());
       }
        return AjaxResult.success("操作成功",flag);
    }


    /**
     * java8 新特性练习
     */
    @ApiOperation(value = "java8新特性查询",notes = "java8新特性查询")
    @PostMapping(value = "/queryJava8ShoppingCart")
    public @ResponseBody AjaxResult queryJava8ShoppingCart(@RequestBody Shoppingcart shoppingcart){
       List<Shoppingcart> shoppingcarts = shoppingcartService.queryJava8ShoppingCart(shoppingcart);
        return AjaxResult.success("操作成功",shoppingcarts);
    }









}
